import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Card, Typography, message, Space } from 'antd';
import { UserOutlined, LockOutlined, MailOutlined } from '@ant-design/icons';
import { Link, useNavigate } from 'react-router-dom';
import useAuthStore from '../stores/authStore';
import './Auth.css';

const { Title, Text } = Typography;

const Login = () => {
  const [form] = Form.useForm();
  const navigate = useNavigate();
  const { login, isLoading } = useAuthStore();
  const [messageApi, contextHolder] = message.useMessage();

  useEffect(() => {
    // 如果已经登录，跳转到控制台
    if (useAuthStore.getState().isAuthenticated) {
      navigate('/dashboard');
    }
  }, [navigate]);

  const onFinish = async (values) => {
    const result = await login(values.email, values.password);
    
    if (result.success) {
      messageApi.success('登录成功！');
      navigate('/dashboard');
    } else {
      messageApi.error(result.error || '登录失败，请重试');
    }
  };

  return (
    <div className="auth-page">
      {contextHolder}
      <div className="auth-container">
        <Card className="auth-card">
          <div className="auth-header">
            <Title level={2} className="auth-title">
              欢迎回来
            </Title>
            <Text type="secondary">
              登录您的账户，开始使用智能去水印服务
            </Text>
          </div>

          <Form
            form={form}
            name="login"
            onFinish={onFinish}
            autoComplete="off"
            size="large"
            className="auth-form"
          >
            <Form.Item
              name="email"
              rules={[
                { required: true, message: '请输入邮箱地址' },
                { type: 'email', message: '请输入有效的邮箱地址' }
              ]}
            >
              <Input
                prefix={<MailOutlined />}
                placeholder="邮箱地址"
                autoComplete="email"
              />
            </Form.Item>

            <Form.Item
              name="password"
              rules={[
                { required: true, message: '请输入密码' },
                { min: 6, message: '密码长度至少6位' }
              ]}
            >
              <Input.Password
                prefix={<LockOutlined />}
                placeholder="密码"
                autoComplete="current-password"
              />
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                loading={isLoading}
                className="auth-button"
                block
              >
                登录
              </Button>
            </Form.Item>
          </Form>

          <div className="auth-footer">
            <Space direction="vertical" size="small" style={{ width: '100%' }}>
              <Text type="secondary">
                还没有账户？{' '}
                <Link to="/register" className="auth-link">
                  立即注册
                </Link>
              </Text>
              <Link to="/forgot-password" className="auth-link">
                忘记密码？
              </Link>
            </Space>
          </div>
        </Card>
      </div>
    </div>
  );
};

export default Login; 